package hc.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnector {

	static final String jdbc_driver = "oracle.jdbc.driver.OracleDriver";
	//static final String DB_URL = "jdbc:oracle:thin:0 E10804:1521:orcl";	//For Vanier College
	static final String DB_URL = "jdbc:oracle:thin:@127.0.0.1:1521:xe";		//For Home
	
	static final String userName = "scott";
	static final String password = "tiger";

	private Connection connection;

	private static DBConnector dbConnector = null;

	private DBConnector() {
		init();
	}

	private void init() {
		try {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());		
			Class.forName(jdbc_driver);
			System.out.println("Connecting to database...");
			connection = DriverManager
					.getConnection(DB_URL, userName, password);
			System.out.println("Create connection = " + connection);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}

	public static Connection getDBConnection() {
		if (dbConnector == null) {
			dbConnector = new DBConnector();
		}

		return dbConnector.connection;
	}

	public void close() {
		if (dbConnector != null) {
			try {
				if (this.connection != null) {
					connection.close();
				}
				dbConnector = null;
			} catch (SQLException | NullPointerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		System.out.println(DBConnector.getDBConnection());
	}

}
